/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MantSeccion.java
 *
 * Created on 12/09/2011, 03:51:58 PM
 */

package GUI.AdministracionProyectos;

import BE.EnunciadoPt;
import BE.PapelTrabajo;
import BE.SeccionPt;
import BL.EnunciadoPtBl;
import BL.PapelTrabajoBl;
import BL.SeccionPtBl;
import GUI.AdministracionPlantillas.*;
import GUI.AdministracionUsuarios.UtilUsuarios;
import GUI.Principal;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author CarLita
 */
public class LlenadoSeccion extends javax.swing.JFrame {
    SeccionPt seccion;
    SeccionPtBl seccionBl;
    PapelTrabajo papelTrabajo;
    EnunciadoPtBl enunciadoPtBl;
    List<SeccionPt> seccionesNivel1;
    SeccionPt seccionNivel1;
    List<SeccionPt> seccionesNivel2;
    SeccionPt seccionNivel2;
    int modoVentana;
    Principal ventanaPrincipal;
    
    public LlenadoSeccion() {
        initComponents();
    }

    public LlenadoSeccion(PapelTrabajo papelTrabajo, int modo, Principal ventanaPrincipal) {
        initComponents();
        modoVentana = modo;
        enunciadoPtBl = new EnunciadoPtBl();
        seccionBl = new SeccionPtBl();
        this.papelTrabajo= papelTrabajo;
        setearModo(modo);
        llenarComboNivel1();
        bloquearPorPermisos();
    }

    public LlenadoSeccion(SeccionPt seccionSeleccionada, int modo, Principal ventanaPrincipal) {
        initComponents();
        seccion = seccionSeleccionada;
        modoVentana = modo;
        enunciadoPtBl = new EnunciadoPtBl();
        seccionBl = new SeccionPtBl();
        papelTrabajo = new PapelTrabajoBl().buscarPorId(seccionSeleccionada.getIdPapelTrabajo());
        setearModo(modo);
        llenarComboNivel1();
        bloquearPorPermisos();
    }

    private void setearModo(int modo){
        switch(modo){
            case Principal.MODO_CREACION:
                btnEditar.setVisible(false);
                btnLimpiar.setVisible(true);
                btnGuardar.setVisible(true);
                bloquearPestaña(true);
                desbloquearEdicion(true);
                break;
            case Principal.MODO_LECTURA:
                btnEditar.setVisible(true);
                btnLimpiar.setVisible(false);
                btnGuardar.setVisible(false);
                llenarInformacion();
                bloquearPestaña(false);
                desbloquearEdicion(false);
                break;
            case Principal.MODO_EDICION:
                btnEditar.setVisible(false);
                btnLimpiar.setVisible(false);
                btnGuardar.setVisible(true);
                llenarInformacion();
                bloquearPestaña(false);
                desbloquearEdicion(true);
                break;
            case Principal.MODO_LLENADO:
                btnEditar.setVisible(false);
                btnLimpiar.setVisible(false);
                btnGuardar.setVisible(true);
                llenarInformacion();
                bloquearPestaña(false);
                desbloquearEdicion(true);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tabSeccion = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        txtTitulo = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        lblNivel1 = new javax.swing.JLabel();
        lblNivel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtDescripcion = new javax.swing.JTextArea();
        jLabel14 = new javax.swing.JLabel();
        cmbNivel1 = new javax.swing.JComboBox();
        cmbNivel2 = new javax.swing.JComboBox();
        btnGuardar = new javax.swing.JButton();
        btnLimpiar = new javax.swing.JButton();
        btnCancelar1 = new javax.swing.JButton();
        jLabel13 = new javax.swing.JLabel();
        lblCorrelativo = new javax.swing.JLabel();
        btnEditar = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblEnunciados = new javax.swing.JTable();
        btnCompletar = new javax.swing.JButton();
        btnAgregar = new javax.swing.JButton();
        btnEliminar = new javax.swing.JButton();
        btnRefrescar = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel12.setText("Título:");

        lblNivel1.setText("Nivel 1:");

        lblNivel2.setText("Nivel2:");

        txtDescripcion.setColumns(20);
        txtDescripcion.setFont(new java.awt.Font("Tahoma", 0, 11));
        txtDescripcion.setRows(5);
        jScrollPane1.setViewportView(txtDescripcion);

        jLabel14.setText("Descripción:");

        cmbNivel1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "-" }));
        cmbNivel1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbNivel1ActionPerformed(evt);
            }
        });

        cmbNivel2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "-" }));
        cmbNivel2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbNivel2ActionPerformed(evt);
            }
        });

        btnGuardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/AdministracionPlantillas/guardar.png"))); // NOI18N
        btnGuardar.setText("Guardar");
        btnGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGuardarActionPerformed(evt);
            }
        });

        btnLimpiar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/AdministracionPlantillas/limpiar.png"))); // NOI18N
        btnLimpiar.setText("Limpiar");
        btnLimpiar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLimpiarActionPerformed(evt);
            }
        });

        btnCancelar1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/AdministracionPlantillas/cancelar.png"))); // NOI18N
        btnCancelar1.setText("Cancelar");
        btnCancelar1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelar1ActionPerformed(evt);
            }
        });

        jLabel13.setText("Correlativo:");

        lblCorrelativo.setText("<por generar>");

        btnEditar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/AdministracionPlantillas/edit.gif"))); // NOI18N
        btnEditar.setText("Editar");
        btnEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(128, 128, 128)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel13, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblCorrelativo, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel14, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 286, Short.MAX_VALUE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel12, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(lblNivel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 89, Short.MAX_VALUE)
                                    .addComponent(lblNivel2, javax.swing.GroupLayout.DEFAULT_SIZE, 89, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(cmbNivel2, 0, 286, Short.MAX_VALUE)
                                    .addComponent(cmbNivel1, 0, 286, Short.MAX_VALUE)
                                    .addComponent(txtTitulo, javax.swing.GroupLayout.PREFERRED_SIZE, 286, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addGap(122, 122, 122))))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(43, Short.MAX_VALUE)
                .addComponent(btnEditar, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnGuardar, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnLimpiar, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnCancelar1, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(40, 40, 40))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtTitulo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel12))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblNivel1, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cmbNivel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblNivel2)
                    .addComponent(cmbNivel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel14)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel13)
                    .addComponent(lblCorrelativo))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 45, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnLimpiar)
                    .addComponent(btnCancelar1)
                    .addComponent(btnGuardar)
                    .addComponent(btnEditar))
                .addGap(21, 21, 21))
        );

        tabSeccion.addTab("Datos Generales", jPanel1);

        jPanel4.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        tblEnunciados.setModel(new DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null}},
            new String [] {
                "Id", "Correlativo", "Enunciado"
            }));
            ocultarIdTabla();
            tblEnunciados.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mousePressed(java.awt.event.MouseEvent evt) {
                    tblEnunciadosMousePressed(evt);
                }
            });
            jScrollPane2.setViewportView(tblEnunciados);

            btnCompletar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/AdministracionProyectos/llenar doc.png"))); // NOI18N
            btnCompletar.setText("<html>  Completar");
            btnCompletar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
            btnCompletar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
            btnCompletar.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btnCompletarActionPerformed(evt);
                }
            });

            btnAgregar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/AdministracionProyectos/agregar.png"))); // NOI18N
            btnAgregar.setText("Agregar");
            btnAgregar.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btnAgregarActionPerformed(evt);
                }
            });

            btnEliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/AdministracionPlantillas/minus white.png"))); // NOI18N
            btnEliminar.setText("Eliminar");
            btnEliminar.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btnEliminarActionPerformed(evt);
                }
            });

            btnRefrescar.setText("Refrescar");
            btnRefrescar.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btnRefrescarActionPerformed(evt);
                }
            });

            javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
            jPanel4.setLayout(jPanel4Layout);
            jPanel4Layout.setHorizontalGroup(
                jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 29, Short.MAX_VALUE)
                    .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(btnAgregar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(btnCompletar))
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(btnRefrescar, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(btnEliminar, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGap(23, 23, 23))
            );
            jPanel4Layout.setVerticalGroup(
                jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel4Layout.createSequentialGroup()
                    .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel4Layout.createSequentialGroup()
                            .addGap(25, 25, 25)
                            .addComponent(btnCompletar, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(btnAgregar)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(btnEliminar)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(btnRefrescar))
                        .addGroup(jPanel4Layout.createSequentialGroup()
                            .addGap(17, 17, 17)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 319, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );

            javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
            jPanel2.setLayout(jPanel2Layout);
            jPanel2Layout.setHorizontalGroup(
                jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addContainerGap())
            );
            jPanel2Layout.setVerticalGroup(
                jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup()
                    .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addContainerGap())
            );

            tabSeccion.addTab("Enunciados", jPanel2);

            jLabel4.setFont(new java.awt.Font("Verdana", 0, 24));
            jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/imagenes/lista-de-regalos.png"))); // NOI18N
            jLabel4.setText("Sección");

            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jLabel4)
                    .addContainerGap(493, Short.MAX_VALUE))
                .addComponent(tabSeccion, javax.swing.GroupLayout.DEFAULT_SIZE, 638, Short.MAX_VALUE)
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(11, 11, 11)
                    .addComponent(jLabel4)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(tabSeccion, javax.swing.GroupLayout.DEFAULT_SIZE, 390, Short.MAX_VALUE))
            );

            pack();
        }// </editor-fold>//GEN-END:initComponents

    private void btnCancelar1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelar1ActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnCancelar1ActionPerformed

    private void btnCompletarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCompletarActionPerformed
        if (tblEnunciados.getSelectedRow() == -1){
            JOptionPane.showMessageDialog(this, "Seleccione el enunciado a completar", "Error", JOptionPane.ERROR_MESSAGE);
            return;
        }
        completarEnunciado();
}//GEN-LAST:event_btnCompletarActionPerformed

    private void btnAgregarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAgregarActionPerformed
        agregarEnunciado();
}//GEN-LAST:event_btnAgregarActionPerformed

    private void btnEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEliminarActionPerformed
        if (tblEnunciados.getSelectedRow()==-1){
            JOptionPane.showMessageDialog(this, "Seleccione una sección para editar", "Error", JOptionPane.ERROR_MESSAGE);
            return;
        }
        eliminarEnunciado();
}//GEN-LAST:event_btnEliminarActionPerformed

    private void btnLimpiarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLimpiarActionPerformed
        txtTitulo.setText("");
        txtDescripcion.setText("");
        cmbNivel1.setSelectedIndex(-1);
        cmbNivel2.setSelectedIndex(-1);
        limpiarTabla();
    }//GEN-LAST:event_btnLimpiarActionPerformed

    private void btnGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGuardarActionPerformed
        guardar();
    }//GEN-LAST:event_btnGuardarActionPerformed

    private void btnEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditarActionPerformed
        setearModo(Principal.MODO_EDICION);
    }//GEN-LAST:event_btnEditarActionPerformed

    private void tblEnunciadosMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblEnunciadosMousePressed
        if (evt.getClickCount()>1){
            mostrarEnunciado();
            buscarEnunciados();
        }
    }//GEN-LAST:event_tblEnunciadosMousePressed

private void btnRefrescarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRefrescarActionPerformed
    llenarEnunciados();
}//GEN-LAST:event_btnRefrescarActionPerformed

private void cmbNivel1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbNivel1ActionPerformed
    if (cmbNivel1.getSelectedIndex()<1){
        llenarComboNivel2(false);
        seccionNivel1=new SeccionPt(-1);
    }
    else {        
        int index = cmbNivel1.getSelectedIndex()-1;
        seccionNivel1 = seccionesNivel1.get(index);
        llenarComboNivel2(true);
    }
}//GEN-LAST:event_cmbNivel1ActionPerformed

private void cmbNivel2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbNivel2ActionPerformed
    if (cmbNivel2.getSelectedIndex()>1){
        int index = cmbNivel2.getSelectedIndex()-1;
        seccionNivel2 = seccionesNivel2.get(index);
    }
    else {
        seccionNivel2=new SeccionPt(-1);
    }
}//GEN-LAST:event_cmbNivel2ActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new LlenadoSeccion().setVisible(true);
            }
        });
    }
    private void ocultarIdTabla() {
        tblEnunciados.getColumn("Id").setWidth(0);
        tblEnunciados.getColumn("Id").setMinWidth(0);
        tblEnunciados.getColumn("Id").setMaxWidth(0);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAgregar;
    private javax.swing.JButton btnCancelar1;
    private javax.swing.JButton btnCompletar;
    private javax.swing.JButton btnEditar;
    private javax.swing.JButton btnEliminar;
    private javax.swing.JButton btnGuardar;
    private javax.swing.JButton btnLimpiar;
    private javax.swing.JButton btnRefrescar;
    private javax.swing.JComboBox cmbNivel1;
    private javax.swing.JComboBox cmbNivel2;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel lblCorrelativo;
    private javax.swing.JLabel lblNivel1;
    private javax.swing.JLabel lblNivel2;
    private javax.swing.JTabbedPane tabSeccion;
    private javax.swing.JTable tblEnunciados;
    private javax.swing.JTextArea txtDescripcion;
    private javax.swing.JTextField txtTitulo;
    // End of variables declaration//GEN-END:variables

    private void completarEnunciado() {
        EnunciadoPt enunciado = getEnunciado();
        new LlenadoEnunciado(enunciado, Principal.MODO_LLENADO, ventanaPrincipal ).setVisible(true);
    }

    private void bloquearPestaña(boolean b) {
        tabSeccion.setEnabledAt(tabSeccion.getTabCount()-1, !b);
    }

    private void llenarInformacion() {
        txtTitulo.setText(seccion.getTitulo());
        txtDescripcion.setText(seccion.getDescripcion());
        lblCorrelativo.setText(""+seccion.getCorrelativo());
        llenarEnunciados();
    }
    
    private void llenarComboNivel1() {        
        seccionesNivel1 = seccionBl.buscarNivelesPorPapelTrabajo(papelTrabajo.getIdPapelTrabajo(),1,0);
        llenarCombo(1);
    }
    
    private void llenarComboNivel2(boolean llenar) {
        if (llenar){
            seccionesNivel2 = seccionBl.buscarNivelesPorPapelTrabajo(papelTrabajo.getIdPapelTrabajo(),2,seccionNivel1.getIdSeccionPt());
            llenarCombo(2);            
        }
        else {
            seccionesNivel2 = new ArrayList<SeccionPt>();
            llenarCombo(2);
        }
    }
    
    private void llenarCombo(int nivel) {
        switch(nivel){
            case 1:
                cmbNivel1.removeAllItems();
                cmbNivel2.addItem("-");
                for (SeccionPt s: seccionesNivel1){
                    cmbNivel1.addItem(s.getTitulo());
                }
                break;
            case 2:
                cmbNivel2.removeAllItems();
                cmbNivel2.addItem("-");
                for (SeccionPt s: seccionesNivel2){
                    cmbNivel2.addItem(s.getTitulo());
                }
                break;
        }
    }

    private void desbloquearEdicion(boolean b) {
        txtTitulo.setEditable(b);
        cmbNivel1.setEditable(b);
        cmbNivel2.setEnabled(b);
        txtDescripcion.setEnabled(b);
        btnCompletar.setEnabled(b);
        btnAgregar.setEnabled(b);
        btnEliminar.setEnabled(b);
    }

    private EnunciadoPt getEnunciado() {
        int idEnunciado = getIdEnunciado();
        return enunciadoPtBl.buscarPorId(idEnunciado);
    }

    private void buscarEnunciados() {
        //buscar los enunciados de la seccion
    }

    private void limpiarTabla() {
        tblEnunciados.setModel(new DefaultTableModel(
            new Object [][] {},
            new String [] {
                "Id", "Correlativo", "Enunciado"
        }));
        ocultarIdTabla();
    }

    private void agregarEnunciado() {
        new LlenadoEnunciado(seccion, ventanaPrincipal).setVisible(true);
    }

    private void eliminarEnunciado() {
        int idEnunciado = getIdEnunciado();
        int opcion = JOptionPane.showConfirmDialog(this, "Está seguro que desea borrar este enunciado?", "Confirmación", JOptionPane.ERROR_MESSAGE);
        if (opcion == JOptionPane.YES_OPTION){
            boolean borrado = enunciadoPtBl.eliminarPorId(idEnunciado);
            if (borrado){
                JOptionPane.showMessageDialog(this, "El enunciado ha sido eliminado", "Confirmacion", JOptionPane.INFORMATION_MESSAGE);
                buscarEnunciados();
            }
            else JOptionPane.showMessageDialog(this, "Ha ocurrido un error en la eliminación.\nPor favor, intente nuevamente.", "Error", JOptionPane.ERROR_MESSAGE);
        }
    }

    private int getIdEnunciado() {
        return Integer.parseInt(tblEnunciados.getValueAt(tblEnunciados.getSelectedRow(), 0).toString());
    }

    private void llenarEnunciados() {
        List enunciados = enunciadoPtBl.buscarPorIdSeccion(seccion.getIdSeccionPt());
        System.out.println("Lista de enunciados - "+enunciados.size());
        llenarTabla(enunciados);
    }

    private void llenarTabla(List enunciados) {
        Vector<String> tableHeaders = new Vector<String>();
        Vector tableData = new Vector();
        tableHeaders.add("Id");
        tableHeaders.add("Correlativo");
        tableHeaders.add("Enunciado");

        EnunciadoPt e;
        for(Object o : enunciados) {
            e = (EnunciadoPt)o;
            System.out.println("Enunciado: "+ e.getEnunciado());
            Vector<Object> fila = new Vector<Object>();
            fila.add(e.getIdEnunciado());
            fila.add(e.getCorrelativo());
            fila.add(e.getEnunciado());
            tableData.add(fila);
        }

        tblEnunciados.setModel(new DefaultTableModel(tableData, tableHeaders));
        ocultarIdTabla();
    }

    private void mostrarEnunciado() {
         EnunciadoPt enunciado = getEnunciado();
        new LlenadoEnunciado(enunciado, Principal.MODO_LECTURA, ventanaPrincipal).setVisible(true);
    }
    
    private String validarCampos() {
        if (txtTitulo.getText().trim().isEmpty())
            return "Usted no ha ingresado el nombre de la sección. ";
        return "";
    }
    
    private boolean grabarSeccion() {
        seccion = new SeccionPt();
        seccion.setTitulo(txtTitulo.getText().trim());
        seccion.setDescripcion(txtDescripcion.getText().trim());
//        seccion.setPapelTrabajo(papelTrabajo);
        seccion.setCorrelativoSuperior1(0);
        seccion.setCorrelativoSuperior2(0);
        seccion.setIdseccionSuperior1(0);
        seccion.setIdseccionSuperior2(0);
                    
       
        seccion.setIdseccionSuperior2(seccionNivel2.getIdSeccionPt());
        seccion.setIdseccionSuperior1(seccionNivel1.getIdSeccionPt());
        
        int auxCorr = seccionNivel1.getIdSeccionPt()!=-1?seccionNivel1.getCorrelativo():0;
        seccion.setCorrelativoSuperior1(auxCorr);
        
        auxCorr = seccionNivel2.getIdSeccionPt()!=-1?seccionNivel2.getCorrelativo():0;
        seccion.setCorrelativoSuperior2(auxCorr);
        
        seccion.setCorrelativo(calcularCorrelativo());        
        int id =  seccionBl.guardar(seccion);        
        seccion.setIdSeccionPt(id);
        return id>-1;

    }
    
    private int calcularCorrelativo() {
        List corr=null;
        if (seccionNivel2!=null && seccionNivel2.getIdSeccionPt()!=-1){
            corr = seccionBl.buscarNivelesPorPapelTrabajo(papelTrabajo.getIdPapelTrabajo(), 3, seccionNivel2.getIdSeccionPt());
            return corr.size()+1;
        }
        if (seccionNivel1!=null && seccionNivel1.getIdSeccionPt()!=-1){
            corr = seccionBl.buscarNivelesPorPapelTrabajo(papelTrabajo.getIdPapelTrabajo(), 2, seccionNivel1.getIdSeccionPt());
            return corr.size()+1;
        }
        Object[] c = seccionBl.buscarPorIdPapelTrabajo(papelTrabajo.getIdPapelTrabajo());
        return corr==null?c.length:corr.size()+1;
    }

    private void guardar() {            
        String mensajeError = validarCampos();
        if (mensajeError.isEmpty()) {
            if (grabarSeccion()){
                setearModo(Principal.MODO_EDICION);
                lblCorrelativo.setText(""+seccion.getCorrelativo());
                JOptionPane.showMessageDialog(this, "La seccion ha sido creada", "Confirmacion", JOptionPane.INFORMATION_MESSAGE);
            }
             else{
                JOptionPane.showMessageDialog(this, "Ha ocurrido un error en la creación. \nPor favor, intente nuevamente.", "Campos incompletos", JOptionPane.ERROR_MESSAGE);
             }
        }
        else {
            JOptionPane.showMessageDialog(this, mensajeError, "Campos incompletos", JOptionPane.ERROR_MESSAGE);
        }    
    }

    private void bloquearPorPermisos() {
       if (ventanaPrincipal.getPermisos().getPermisoProyecto().indexOf(UtilUsuarios.VER)>-1){
            btnGuardar.setVisible(false);
            btnGuardar.setEnabled(false);
            btnEditar.setEnabled(false);
            btnEditar.setVisible(false);
            btnAgregar.setVisible(false);
            btnAgregar.setEnabled(false);
            btnCompletar.setEnabled(false);
            btnCompletar.setVisible(false);
            
            
        }        
        if (ventanaPrincipal.getPermisos().getPermisoProyecto().indexOf(UtilUsuarios.CREAR)>-1){
            btnGuardar.setVisible(true);
            btnGuardar.setEnabled(true);
            btnEditar.setEnabled(false);
            btnEditar.setVisible(false);
            btnAgregar.setVisible(true);
            btnAgregar.setEnabled(true);
            btnCompletar.setEnabled(false);
            btnCompletar.setVisible(false);
            btnEliminar.setEnabled(true);
            btnEliminar.setVisible(true);
            
        }                
        if (ventanaPrincipal.getPermisos().getPermisoProyecto().indexOf(UtilUsuarios.EDITAR)>-1){
            btnGuardar.setVisible(true);
            btnGuardar.setEnabled(true);
            btnAgregar.setVisible(true);
            btnAgregar.setEnabled(true);
            btnEditar.setEnabled(true);
            btnEditar.setVisible(true);
            btnCompletar.setEnabled(false);
            btnCompletar.setVisible(false);
            btnEliminar.setEnabled(true);
            btnEliminar.setVisible(true);
            
        }
         if (ventanaPrincipal.getPermisos().getPermisoProyecto().indexOf(UtilUsuarios.COMPLETAR)>-1){
            btnGuardar.setVisible(true);
            btnGuardar.setEnabled(true);
            btnAgregar.setVisible(true);
            btnAgregar.setEnabled(true);
            btnEditar.setEnabled(true);
            btnEditar.setVisible(true);
            btnCompletar.setEnabled(true);
            btnCompletar.setVisible(true);
            btnEliminar.setEnabled(true);
            btnEliminar.setVisible(true);
        }
    }
}
